﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Input;

namespace HuntTheWumpus
{
	public partial class GameForm : Form
	{
		Character character;
		Boolean isUp = false;
		Boolean isDown = false;
		Boolean isRight = false;
		Boolean isLeft = false;
		int movedRooms = -1;
		public PictureBox[] doors = new PictureBox[6];
		int[] openDoors;
		public string playername;

		public GameForm()
		{
			GameControl instantiate = new GameControl();
			InitializeComponent();
			this.Select();
			doors[0] = pictureBoxDoor1;
			doors[1] = pictureBoxDoor2;
			doors[2] = pictureBoxDoor3;
			doors[3] = pictureBoxDoor4;
			doors[4] = pictureBoxDoor5;
			doors[5] = pictureBoxDoor6;
			openDoors = GameControl.GetConnections(GameControl.map.PlayerLocation);
			character = new Character(pictureBoxCharacter);
			GameControl.DoTrivia(1);

			CreateRoom(openDoors);
			labelName.Text = playername;
			this.KeyPreview = true;
			this.PreviewKeyDown += new PreviewKeyDownEventHandler(GameForm_PreviewKeyDown);
			this.KeyDown +=
				new KeyEventHandler(GameForm_KeyDown);
			this.KeyUp +=
				new KeyEventHandler(GameForm_KeyUp);
			UpdateGameForm(GameControl.UIInformation());
			instantiate.CreatePlayer("Player");
		}

		public void GameForm_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
		{
			switch (e.KeyCode)
			{
				case Keys.Right:
				case Keys.Left:
				case Keys.Down:
				case Keys.Up:
					e.IsInputKey = true;
					break;
			}
		}

		public void GameForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Up)
			{
				isUp = true;
			}
			if (e.KeyCode == Keys.Down)
			{
				isDown = true;
			}
			if (e.KeyCode == Keys.Right)
			{
				isRight = true;
			}
			if (e.KeyCode == Keys.Left)
			{
				isLeft = true;
			}
			movedRooms = character.Move(isUp, isDown, isRight, isLeft, openDoors);
			if (movedRooms != -1)
			{
				GameControl.MovePlayer(movedRooms);
				openDoors = GameControl.GetConnections(movedRooms);
				CreateRoom(openDoors);
				GameControl.playerMoved();
				UpdateGameForm(GameControl.UIInformation());
			}

		}

		public void GameForm_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.Up)
			{
				isUp = false;
			}
			if (e.KeyCode == Keys.Down)
			{
				isDown = false;
			}
			if (e.KeyCode == Keys.Right)
			{
				isRight = false;
			}
			if (e.KeyCode == Keys.Left)
			{
				isLeft = false;
			}
			movedRooms = character.Move(isUp, isDown, isRight, isLeft, openDoors);
			if (movedRooms != -1)
			{
				GameControl.MovePlayer(movedRooms);
				openDoors = GameControl.GetConnections(movedRooms);
				CreateRoom(openDoors);
				GameControl.playerMoved();

				UpdateGameForm(GameControl.UIInformation());
			}
		}

		public void CreateRoom(int[] d)
		{
			for (int index = 0; index < 6; index++)
			{
				if (d[index] != -1)
				{
					doors[index].Visible = true;
				}
				else
				{
					doors[index].Visible = false;
				}
			}
			character.ResetCharacter();
		}

		public void UpdateGameForm(List<String> i)
		{
			labelGold.Text = "Gold: " + i[0];
			labelArrows.Text = "Arrows: " + i[1];
			labelTurnNumber.Text = "Turn Number: " + i[2];
			labelRoomNumber.Text = "Room Number: " + i[3];
			hintLabel.Text = i[4];

		}

		private void buttonShootArrow_Click(object sender, EventArgs e)
		{
			buttonShootArrow1.Visible = true;
			buttonShootArrow2.Visible = true;
			buttonShootArrow3.Visible = true;
			buttonShootArrow4.Visible = true;
			buttonShootArrow5.Visible = true;
			buttonShootArrow6.Visible = true;

			buttonShootArrow1.Enabled = true;
			buttonShootArrow2.Enabled = true;
			buttonShootArrow3.Enabled = true;
			buttonShootArrow4.Enabled = true;
			buttonShootArrow5.Enabled = true;
			buttonShootArrow6.Enabled = true;

			buttonCancelShot.Enabled = true;
		}

		private void buttonExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void buttonSelectCharacter_Click(object sender, EventArgs e)
		{
			pictureBoxCharacter.Focus();
		}

		private void buttonShootArrow1_Click(object sender, EventArgs e)
		{
			ShootArrow(1);
		}

		private void buttonShootArrow2_Click(object sender, EventArgs e)
		{
			ShootArrow(2);
		}

		private void buttonShootArrow3_Click(object sender, EventArgs e)
		{
			ShootArrow(3);
		}

		private void buttonShootArrow4_Click(object sender, EventArgs e)
		{
			ShootArrow(4);
		}

		private void buttonShootArrow5_Click(object sender, EventArgs e)
		{
			ShootArrow(5);
		}

		private void buttonShootArrow6_Click(object sender, EventArgs e)
		{
			ShootArrow(6);
		}

		public void ShootArrow(int door)
		{
			MessageBox.Show("You Win!");
			buttonShootArrow1.Visible = false;
			buttonShootArrow2.Visible = false;
			buttonShootArrow3.Visible = false;
			buttonShootArrow4.Visible = false;
			buttonShootArrow5.Visible = false;
			buttonShootArrow6.Visible = false;

			buttonShootArrow1.Enabled = false;
			buttonShootArrow2.Enabled = false;
			buttonShootArrow3.Enabled = false;
			buttonShootArrow4.Enabled = false;
			buttonShootArrow5.Enabled = false;
			buttonShootArrow6.Enabled = false;
		}

		private void buttonCancelShot_Click(object sender, EventArgs e)
		{
			buttonShootArrow1.Visible = false;
			buttonShootArrow2.Visible = false;
			buttonShootArrow3.Visible = false;
			buttonShootArrow4.Visible = false;
			buttonShootArrow5.Visible = false;
			buttonShootArrow6.Visible = false;

			buttonShootArrow1.Enabled = false;
			buttonShootArrow2.Enabled = false;
			buttonShootArrow3.Enabled = false;
			buttonShootArrow4.Enabled = false;
			buttonShootArrow5.Enabled = false;
			buttonShootArrow6.Enabled = false;

			buttonCancelShot.Enabled = false;
		}
	}
}
